#include "MCF52259_ADC_driver.h"



void ADC_init(void)
{
	MCF_GPIO_PANPAR=MCF_GPIO_PANPAR_AN0_AN0
					|MCF_GPIO_PANPAR_AN1_AN1
					|MCF_GPIO_PANPAR_AN2_AN2
					|MCF_GPIO_PANPAR_AN3_AN3
					|MCF_GPIO_PANPAR_AN4_AN4
					|MCF_GPIO_PANPAR_AN5_AN5
					|MCF_GPIO_PANPAR_AN6_AN6
					|MCF_GPIO_PANPAR_AN7_AN7;
	
	MCF_ADC_CTRL1=MCF_ADC_CTRL1_STOP0
				 |MCF_ADC_CTRL1_CHNCFG(0)
				 |MCF_ADC_CTRL1_SMODE(0)
				 |MCF_ADC_CTRL1_EOSIE0;
				 //|MCF_ADC_CTRL1_ZCIE
				 //|MCF_ADC_CTRL1_LLMTIE
				 //|MCF_ADC_CTRL1_HLMTIE;
	
	MCF_ADC_CTRL2=MCF_ADC_CTRL2_DIV(10);//divisor=2*(3+10)=??
	
	MCF_ADC_CTRL2=MCF_ADC_CTRL2_STOP1
				 |MCF_ADC_CTRL2_SYNC1
				 |MCF_ADC_CTRL2_EOSIE1;

	MCF_ADC_ADZCC=0;//MCF_ADC_ADZCC_ZCE0(1);
//				 |MCF_ADC_ADZCC_ZCE1(3)
//				 |MCF_ADC_ADZCC_ZCE2(3)
//				 |MCF_ADC_ADZCC_ZCE3(3)
//				 |MCF_ADC_ADZCC_ZCE4(3)
//				 |MCF_ADC_ADZCC_ZCE5(3)
//				 |MCF_ADC_ADZCC_ZCE6(3)
//				 |MCF_ADC_ADZCC_ZCE7(3)
	
	MCF_ADC_ADLST1=MCF_ADC_ADLST1_SAMPLE0(0)
				  |MCF_ADC_ADLST1_SAMPLE1(1)
				  |MCF_ADC_ADLST1_SAMPLE2(2)
				  |MCF_ADC_ADLST1_SAMPLE3(3);
				  
	MCF_ADC_ADLST2=MCF_ADC_ADLST2_SAMPLE4(4)
				  |MCF_ADC_ADLST2_SAMPLE5(5)
				  |MCF_ADC_ADLST2_SAMPLE6(6)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ADOFS_OFFSET(0);
	MCF_ADC_ADOFS(4)=MCF_ADC_ADOFS_OFFSET(0);
	MCF_ADC_ADOFS(5)=MCF_ADC_ADOFS_OFFSET(0);
	MCF_ADC_ADOFS(6)=MCF_ADC_ADOFS_OFFSET(0);
	MCF_ADC_ADOFS(7)=MCF_ADC_ADOFS_OFFSET(0);
	
	MCF_ADC_ADZCSTAT=0XFF;
	
	MCF_ADC_POWER|=MCF_ADC_POWER_ASB;
	
//set interrupt for ADCA	
	MCF_INTC0_IMRH&=~MCF_INTC_IMRH_INT_MASK49;
	MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
	MCF_INTC0_ICR49=MCF_INTC_ICR_IP(3)+MCF_INTC_ICR_IL(4);

//set interrupt for	ADCB
	MCF_INTC0_IMRH&=~MCF_INTC_IMRH_INT_MASK50;
	MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
	MCF_INTC0_ICR50=MCF_INTC_ICR_IP(3)+MCF_INTC_ICR_IL(4);

//set interrupt for	ZCI & LLMIT & HLMIT
//	MCF_INTC0_IMRH&=~MCF_INTC_IMRH_INT_MASK51;
//	MCF_INTC0_IMRL&=~MCF_INTC_IMRL_MASKALL;
//	MCF_INTC0_ICR51=MCF_INTC_ICR_IP(3)+MCF_INTC_ICR_IL(4);
	
}

void ADC_start(void)
{
	MCF_ADC_POWER&=~MCF_ADC_POWER_PD0;
	while(MCF_ADC_POWER&MCF_ADC_POWER_PSTS0) ;
	
	MCF_ADC_CTRL1&=~MCF_ADC_CTRL1_STOP0;
	MCF_ADC_CTRL1|=MCF_ADC_CTRL1_START0;
	
	MCF_ADC_POWER&=~MCF_ADC_POWER_PD1;
	while(MCF_ADC_POWER&MCF_ADC_POWER_PSTS1) ;
	
	MCF_ADC_CTRL2&=~MCF_ADC_CTRL2_STOP1;
	MCF_ADC_CTRL2|=MCF_ADC_CTRL2_START1;
}

void ADC_stop(void)
{
	MCF_ADC_CTRL1|=MCF_ADC_CTRL1_STOP0;
}

//source:49 ADCA conversion complete
__declspec(interrupt) void ADC0(void) 
{
	static unsigned int AD5;
	CT_INT_ADC0++;
	if(CT_INT_ADC0%225000==0)
		change(LED,LED1);

	AD5=MCF_ADC_ADRSLT5;
	if((MCF_ADC_ADRSLT5&0x7FF8)>10040)
	{
		set(LED,LED3);
	}
	else
	{
		clr(LED,LED3);
	}
	
	MCF_ADC_ADSTAT|=MCF_ADC_ADSTAT_EOSI0;
	
}

//source:50 ADCB conversion complete
__declspec(interrupt)void ADC1(void)
{
	CT_INT_ADC1++;
	change(LED,LED2);
	/************************************
	 * 
	 *  	This interrupt should not be
	 *	used in usual.
	 */
	MCF_ADC_ADSTAT|=MCF_ADC_ADSTAT_EOSI1;	
}

//source:51 ADC  interrupt of ZCI LLMIT HLMIT
__declspec(interrupt)void ADCRANGE(void)
{
	CT_INT_ADCRANGE++;
	if(MCF_ADC_ADZCSTAT&MCF_ADC_ADZCSTAT_ZCS0)
	{
		MCF_ADC_ADZCSTAT|=MCF_ADC_ADZCSTAT_ZCS0;
	}
	if(MCF_ADC_ADLSTAT&MCF_ADC_ADLSTAT_LLS0)
	{
		MCF_ADC_ADLSTAT|=MCF_ADC_ADLSTAT_LLS0;
	}
	if(MCF_ADC_ADLSTAT&MCF_ADC_ADLSTAT_HLS0)
	{
		MCF_ADC_ADLSTAT|=MCF_ADC_ADLSTAT_HLS0;
	}
	/************************************
	 * 
	 *  	This interrupt should not be
	 *	used in usual.
	 */
}